/* Copyright (c) 2007-2010 SmartVault Corporation, all rights reserved.
 * 
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
// TestToolbarDlg.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"		// main symbols
#include "TestToolbarDlg.h"

#include "..\..\SDK\Include\SmartVaultDispatchSDK.h"
using namespace SmartVault;

// Constants:
#define APPLICATION_TYPE _T("SmartVault-Sample")
#define TYPE_ID_HOME _T("Home")
#define TYPE_ID_CUSTOMER _T("Customer")

// TestToolbarDlg dialog

IMPLEMENT_DYNAMIC(TestToolbarDlg, CDialog)

TestToolbarDlg::TestToolbarDlg(LPCTSTR lpContextName, LPCTSTR lpConfigFile, CWnd* pParent /*=NULL*/)
	: CDialog(TestToolbarDlg::IDD, pParent), m_contextId(lpContextName), m_configName(lpContextName)
{
    m_customerCountKey.LoadStringW(IDS_LASTCOUNT);
}

TestToolbarDlg::~TestToolbarDlg()
{
    m_lbItems.Detach();
    m_edtName.Detach();
    m_btnSave.Detach();
    m_btnCancel.Detach();
    
    SVToolbar.Detach();
}

void TestToolbarDlg::OnDestroy()
{
    SVContext->Close();
    CDialog::OnDestroy();
    delete(this);
}

void TestToolbarDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(TestToolbarDlg, CDialog)
    ON_WM_DESTROY()
    ON_WM_SIZE()
    ON_BN_CLICKED(IDC_RADIO_NONE, &TestToolbarDlg::ViewNone)
    ON_BN_CLICKED(IDC_RADIO_NONID, &TestToolbarDlg::ViewNonIdentity)
    ON_BN_CLICKED(IDC_RADIO_ITEM, &TestToolbarDlg::ViewIdentity)
    ON_BN_CLICKED(IDC_RADIO_NEW, &TestToolbarDlg::ViewNew)
//    ON_WM_CREATE()
ON_BN_CLICKED(IDC_BUTTON_SAVE_NEW, &TestToolbarDlg::OnBnClickedSaveNew)
ON_BN_CLICKED(IDC_BUTTON_CANCEL_NEW, &TestToolbarDlg::OnBnClickedAbandonNew)
ON_LBN_SELCHANGE(IDC_LISTITEMS, &TestToolbarDlg::OnLbnSelchangeListitems)
END_MESSAGE_MAP()

// TestToolbarDlg message handlers

BOOL TestToolbarDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

    SetWindowText(m_contextId);

    ShowWindow(SW_SHOW);

    // Create Toolbar:
    CRect area(0, 0, 50, 50);
    CString wintitle("");

    TRY
    {
        CString app(APPLICATION_TYPE);
        SVToolbar.Create(wintitle, WS_CHILDWINDOW | WS_CLIPSIBLINGS | WS_VISIBLE, area, this, IDC_TOOLBAR1);
        SVContext = SVToolbar.Open(app, m_contextId, m_contextId);
	}
	CATCH_ALL(e)
	{
		TRACE(traceAppMsg, 0, "Warning: Exception in Open.\n");
    }
	END_CATCH_ALL

    // Reposition Controls:    
    GetClientRect(&area);
    ResizeTo(area.Width(), area.Height());

    m_lbItems.Attach(::GetDlgItem(m_hWnd, IDC_LISTITEMS));
    m_edtName.Attach(::GetDlgItem(m_hWnd, IDC_EDITNEW));
    m_btnSave.Attach(::GetDlgItem(m_hWnd, IDC_BUTTON_SAVE_NEW));
    m_btnCancel.Attach(::GetDlgItem(m_hWnd, IDC_BUTTON_CANCEL_NEW));
    
    int count = ::GetPrivateProfileIntW(m_contextId, m_customerCountKey, 1, m_configName);
    for(int i=0; i < count; i++ )
    {
        CString id, name;
        id.Format(IDS_FORMATCUSTOMERID, i + 1);
        name.Format(IDS_FORMATCUSTOMERNAME, i + 1);
        
        GetPrivateProfileString(m_contextId, id, name, name.GetBuffer(1024), 1024, m_configName);
        m_lbItems.AddString(name);
    }

    SendDlgItemMessage(IDC_RADIO_NONE, BM_SETCHECK, BST_CHECKED, 0);

    CenterWindow();
    return TRUE;
}

void TestToolbarDlg::ResizeTo(int cx, int cy)
{
    const int TB_WIDTH = 58;
    SVToolbar.SetWindowPos(&wndTopMost, cx - TB_WIDTH, 0, TB_WIDTH, cy, SWP_NOACTIVATE);

    CWnd *p = GetDlgItem(IDC_GROUP1);
    WINDOWPLACEMENT wp;
    memset(&wp, 0, sizeof(wp));
    wp.length = sizeof(wp);
    
    p->GetWindowPlacement(&wp);
    wp.rcNormalPosition.top = 5;
    wp.rcNormalPosition.left = 5;
    wp.rcNormalPosition.bottom = cy - 10;
    wp.rcNormalPosition.right = cx - 70;
    p->SetWindowPlacement(&wp);
}

void TestToolbarDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialog::OnSize(nType, cx, cy);
    
    if(SVToolbar.m_hWnd != NULL)
        ResizeTo(cx, cy);
}

void TestToolbarDlg::DisableAll()
{
    m_lbItems.EnableWindow(false);
    m_edtName.EnableWindow(false);
    m_btnSave.EnableWindow(false);
    m_btnCancel.EnableWindow(false);
    
    if(SVNewItem != NULL)
        SVNewItem.Release();
}

void TestToolbarDlg::ViewNone()
{
    DisableAll();
    SVContext->ClearCurrentItem();
}

void TestToolbarDlg::ViewNonIdentity()
{
    DisableAll();
    CToolbarMessagePtr msg(SVContext->CreateMessage());
    msg->SetObjectType(TYPE_ID_HOME);
    msg->Commit();
}

void TestToolbarDlg::ViewIdentity()
{
    DisableAll();
    m_lbItems.EnableWindow(true);
    INT selIx = m_lbItems.GetCurSel();
    if(selIx < 0) 
        m_lbItems.SetSel(selIx = 0);

    CString id, name, desc;
    id.Format(IDS_FORMATCUSTOMERID, selIx + 1);
    m_lbItems.GetText(selIx, name);
    desc.Format(IDS_FORMATDESC, name, m_contextId);

    CToolbarMessagePtr msg(SVContext->CreateMessage());

    msg->SetObjectTypeWithIdentity(TYPE_ID_CUSTOMER, _bstr_t(id));
    msg->SetSuggestedName(_bstr_t(name), _bstr_t(desc));
    msg->Commit();
}

void TestToolbarDlg::ViewNew()
{
    DisableAll();
    m_edtName.EnableWindow(true);
    m_btnSave.EnableWindow(true);
    m_btnCancel.EnableWindow(true);

    INT nextId = 1 + m_lbItems.GetCount();
    CString name;
    name.Format(IDS_FORMATCUSTOMERNAME, nextId);
    m_edtName.SetWindowText(name);

    CToolbarMessagePtr msg(SVContext->CreateMessage());
    SVNewItem.Attach(msg->SetNewItem(TYPE_ID_CUSTOMER));
    msg->Commit();
}

void TestToolbarDlg::OnBnClickedSaveNew()
{
    INT nextId = 1 + m_lbItems.GetCount();
    CString id, name, desc;
    id.Format(IDS_FORMATCUSTOMERID, nextId);
    m_edtName.GetWindowText(name);
    desc.Format(IDS_FORMATDESC, name, m_contextId);
    WritePrivateProfileStringW(m_contextId, id, name, m_configName);

    SVNewItem->SetSuggestedName(_bstr_t(name), _bstr_t(desc));
    SVNewItem->Commit(_bstr_t(id));

    m_lbItems.AddString(name);
    CString count;
    count.Format(_T("%ld"), nextId);
    WritePrivateProfileStringW(m_contextId, m_customerCountKey, count, m_configName);

    ViewNew();
}

void TestToolbarDlg::OnBnClickedAbandonNew()
{
    SVNewItem->AbandonItem();
 
    ViewNew();
}

void TestToolbarDlg::OnLbnSelchangeListitems()
{
    ViewIdentity();
}
